package com.py.credit.service.impl;

import com.py.credit.constant.ResourcePath;
import com.py.credit.dao.CreditReportCustomDao;
import com.py.credit.entity.ReportCustom;
import com.py.credit.service.CreditReportCustomService;
import com.py.framework.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 服务实现
 *
 * @author YaoS
 * @date 2019-04-24 10:15
 */
@Service
public class CreditReportCustomServiceImpl implements CreditReportCustomService {
    @Autowired
    private CreditReportCustomDao customDao;

    /**
     * no:报告编号 productName：制作方 reportStatement：报告声明 waterMark：水印图
     * <p>
     * 片
     */
    @Override
    public void addReportConfig(String watermarker,String dept,
                                CommonsMultipartFile reportWatermark, CommonsMultipartFile enterpriseIcon,String bgbh,String reportStatement) throws IOException {
        File dirPath = new File(watermarker);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        if(reportWatermark != null) {
        	this.surePic(reportWatermark);
        	reportWatermark.transferTo(new File(watermarker + ResourcePath.WATERMAERK_NAME));
        }
        if(enterpriseIcon != null) {
        	this.surePic(enterpriseIcon);
        	enterpriseIcon.transferTo(new File(watermarker + ResourcePath.ENTERPRISE_ICON_NAME));
        }
        ReportCustom record = new ReportCustom(dept, ResourcePath.WATERMAERK_IMAGES_DIR + ResourcePath.WATERMAERK_NAME,
                ResourcePath.WATERMAERK_IMAGES_DIR + ResourcePath.ENTERPRISE_ICON_NAME);
        record.setBgbh(bgbh);
        record.setReportStatement(reportStatement);
        ReportCustom selectLatest = customDao.selectLatest();
        if (selectLatest != null) {
            record.setId(selectLatest.getId());
            customDao.updateByPrimaryKeySelective(record);
        } else {
            customDao.upload(record);
        }

    }

    private void surePic(CommonsMultipartFile waterMark) throws IOException {
        if (waterMark != null) {
            BufferedImage image = ImageIO.read

                    (waterMark.getInputStream());
            if (image != null) {
                int width = image.getWidth();
                int height = image.getHeight();
                if (width > 1080 || height > 1920) {
                    throw new ServiceException("图片图尺寸不超过1080*1920Px");
                }
            } else {
                throw new ServiceException("上传文件非图片,请重新上传");
            }
            if (!"image/png".equals(waterMark.getContentType()) && !"image/jpeg".equals(waterMark.getContentType())
                    && !"image/jpg".equals(waterMark.getContentType())) {
                throw new ServiceException("图片必须是JPG、JEPG、PNG 格式");
            }
            if (waterMark.getSize() > 3 * 1024  * 1024) {
                throw new ServiceException("图片不能超过3M");
            }
        }
    }

    @Override
    public ReportCustom getLatest() {
        return customDao.selectLatest();
    }

}
